﻿<Window x:Class="JenkinsController.View.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:controls="clr-namespace:JenkinsController.View.Controls"
        Title="Jenkins Controller" Height="500" Width="700"
        DataContext="{Binding Main, Source={StaticResource Locator}}"
        Visibility="{Binding IsVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
        Icon="..\Resources\Moustache.ico" Closing="OnWindowClosing" PreviewKeyDown="OnPreviewKeyDown">
    <Window.Resources>

    </Window.Resources>

    <DockPanel LastChildFill="True">

        <!-- MENU -->
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="File">
                <MenuItem Header="Server Manager..." Command="{Binding ServerManagerCommand}" InputGestureText="Ctrl+S">
                    <MenuItem.Icon>
                        <Image Source="..\Resources\Settings.ico" Width="16" Height="16" />
                    </MenuItem.Icon>
                </MenuItem>
                <Separator />
                <MenuItem Header="Quit" Command="{Binding QuitCommand}" />
            </MenuItem>

            <MenuItem Header="Help">
                <MenuItem Header="View Help" InputGestureText="F1" Command="{Binding HelpCommand}" />
                <Separator />
                <MenuItem Header="About Jenkins Controller" Command="{Binding AboutCommand}" />
            </MenuItem>
        </Menu>

        <!-- TOOLBAR -->
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar Band="1" BandIndex="1">
                <Button Command="{Binding ServerManagerCommand}" ToolTip="Show Server Manager (Ctrl+S)">
                    <Image Source="..\Resources\Settings.ico" Width="16" Height="16" />
                </Button>
                <ComboBox ItemsSource="{Binding Servers}" DisplayMemberPath="Node.Description"
                          IsEditable="False"
                          SelectedItem="{Binding SelectedServer, Mode=TwoWay}"
                          VerticalAlignment="Center" />
            </ToolBar>

            <ToolBar Band="1" BandIndex="2"
                     Visibility="{Binding IsSelected, ElementName=JobsTab, Converter={StaticResource BooleanToVisibilityConverter}}">
                <Button Command="{Binding RunJobCommand}" CommandParameter="{Binding SelectedJob}"
                        ToolTip="Start a new build on selected job.">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="10" Height="10" Source="..\Resources\Play.ico" />
                        <TextBlock Margin="5,0,0,0">Run</TextBlock>
                    </StackPanel>
                </Button>
                <Button Command="{Binding StopJobCommand}" CommandParameter="{Binding SelectedJob}"
                        ToolTip="Stop running build of selected job.">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="10" Height="10" Source="..\Resources\Stop.ico" />
                        <TextBlock Margin="5,0,0,0">Stop</TextBlock>
                    </StackPanel>
                </Button>
                <Separator />
                <Button Command="{Binding ChangeSetCommand}" CommandParameter="{Binding SelectedJob.LastBuild}"
                        ToolTip="Show source controller change set of selected job last build.">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="16" Height="16" Source="..\Resources\ChangeSet.ico" />
                        <TextBlock Margin="5,0,0,0">Change set</TextBlock>
                    </StackPanel>
                </Button>
                <Button Command="{Binding LogsCommand}" CommandParameter="{Binding SelectedJob.LastBuild}"
                        ToolTip="Show live logs of selected job last build.">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="16" Height="16" Source="..\Resources\Logs.ico" />
                        <TextBlock Margin="5,0,0,0">Logs</TextBlock>
                    </StackPanel>
                </Button>
            </ToolBar>

        </ToolBarTray>

        <!-- STATUS BAR -->
        <StatusBar DockPanel.Dock="Bottom">
            <StatusBar.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </StatusBar.ItemsPanel>

            <StatusBarItem>
                <TextBlock>
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsLoading}" Value="True">
                                    <Setter Property="Text" Value="Loading..." />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </StatusBarItem>

            <StatusBarItem Grid.Column="1">
                <TextBlock Text="{Binding LastErrorMessage}" Foreground="Red">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding LastErrorMessage}" Value="{x:Null}">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </StatusBarItem>

            <StatusBarItem Grid.Column="2">
                <TextBlock>
                    <Run>Last update:</Run>
                    <Run Text="{Binding LastUpdate, StringFormat={}{0:HH\\:mm\\:ss}}" />
                </TextBlock>
            </StatusBarItem>
        </StatusBar>

        <!-- TABS -->
        <TabControl DataContext="{Binding SelectedServer}">

            <!-- NODE INFORMATION -->
            <TabItem Header="Node Information">
                <Grid Margin="10">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="10" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <TextBlock Grid.Row="0" HorizontalAlignment="Right">Mode</TextBlock>
                    <TextBlock Grid.Row="1" HorizontalAlignment="Right">Node description</TextBlock>
                    <TextBlock Grid.Row="2" HorizontalAlignment="Right">Node name</TextBlock>
                    <TextBlock Grid.Row="3" HorizontalAlignment="Right">Number of executors</TextBlock>
                    <TextBlock Grid.Row="4" HorizontalAlignment="Right">Description</TextBlock>
                    <TextBlock Grid.Row="5" HorizontalAlignment="Right">Quieting Down</TextBlock>
                    <TextBlock Grid.Row="6" HorizontalAlignment="Right">Use Security</TextBlock>

                    <TextBlock Grid.Column="2" Grid.Row="0" FontWeight="Bold" Text="{Binding Node.Mode}" />
                    <TextBlock Grid.Column="2" Grid.Row="1" FontWeight="Bold" Text="{Binding Node.NodeDescription}" />
                    <TextBlock Grid.Column="2" Grid.Row="2" FontWeight="Bold" Text="{Binding Node.NodeName}" />
                    <TextBlock Grid.Column="2" Grid.Row="3" FontWeight="Bold" Text="{Binding Node.NumExecutors}" />
                    <TextBlock Grid.Column="2" Grid.Row="4" FontWeight="Bold" Text="{Binding Node.Description}" />
                    <TextBlock Grid.Column="2" Grid.Row="5" FontWeight="Bold" Text="{Binding Node.IsQuietingDown}" />
                    <TextBlock Grid.Column="2" Grid.Row="6" FontWeight="Bold" Text="{Binding Node.UseSecurity}" />
                </Grid>
            </TabItem>

            <!-- JOBS -->
            <TabItem IsSelected="True" Name="JobsTab">
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Margin="0,0,5,0" VerticalAlignment="Center">Jobs</TextBlock>
                        <controls:JobStatusFilterControl
                            SelectedJobStatus="{Binding Main.SelectedJobStatus, Source={StaticResource Locator}, Mode=TwoWay}" />
                    </StackPanel>
                </TabItem.Header>
                <TabControl ItemsSource="{Binding Node.Views}" DisplayMemberPath="Name"
                            SelectedItem="{Binding Main.SelectedView, Source={StaticResource Locator}}">

                    <TabControl.ContentTemplate>
                        <DataTemplate>

                            <ListView ItemsSource="{Binding Main.FilteredJobs, Source={StaticResource Locator}}"
                                      SelectedItem="{Binding Main.SelectedJob, Source={StaticResource Locator}, Mode=TwoWay}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseDoubleClick">
                                        <i:InvokeCommandAction
                                            Command="{Binding Main.JobCommand, Source={StaticResource Locator}}"
                                            CommandParameter="{Binding Main.SelectedJob, Source={StaticResource Locator}}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>

                                <ListView.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="Run"
                                                  Command="{Binding Main.RunJobCommand, Source={StaticResource Locator}}"
                                                  CommandParameter="{Binding Main.SelectedJob, Source={StaticResource Locator}}">
                                            <MenuItem.Icon>
                                                <Image Width="16" Height="16" Source="..\Resources\Play.ico" />
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <MenuItem Header="Stop"
                                                  Command="{Binding Main.StopJobCommand, Source={StaticResource Locator}}"
                                                  CommandParameter="{Binding Main.SelectedJob, Source={StaticResource Locator}}">
                                            <MenuItem.Icon>
                                                <Image Width="16" Height="16" Source="..\Resources\Stop.ico" />
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <Separator />
                                        <MenuItem Header="Artifacts"
                                                  ItemsSource="{Binding Main.SelectedJob.LastBuild.Artifacts, Source={StaticResource Locator}}"
                                                  DisplayMemberPath="FileName">
                                            <MenuItem.ItemContainerStyle>
                                                <Style TargetType="MenuItem">
                                                    <Setter Property="Command"
                                                            Value="{Binding Main.DownloadArtifactCommand, Source={StaticResource Locator}}" />
                                                    <Setter Property="CommandParameter" Value="{Binding}" />
                                                </Style>
                                            </MenuItem.ItemContainerStyle>
                                        </MenuItem>
                                        <MenuItem Header="Changes"
                                                  Command="{Binding Main.ChangeSetCommand, Source={StaticResource Locator}}"
                                                  CommandParameter="{Binding Main.SelectedJob.LastBuild, Source={StaticResource Locator}}">
                                            <MenuItem.Icon>
                                                <Image Source="..\Resources\ChangeSet.ico" Width="16" Height="16" />
                                            </MenuItem.Icon>
                                        </MenuItem>
                                        <Separator />
                                        <MenuItem Header="Logs"
                                                  Command="{Binding Main.LogsCommand, Source={StaticResource Locator}}"
                                                  CommandParameter="{Binding Main.SelectedJob.LastBuild, Source={StaticResource Locator}}">
                                            <MenuItem.Icon>
                                                <Image Source="..\Resources\Logs.ico" Width="16" Height="16" />
                                            </MenuItem.Icon>
                                        </MenuItem>
                                    </ContextMenu>
                                </ListView.ContextMenu>

                                <ListView.View>
                                    <GridView>
                                        <GridView.Columns>

                                            <GridViewColumn Header="Status">
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <controls:JobStatusControl Status="{Binding Status}" />
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                                            <GridViewColumn Header="Name"
                                                            DisplayMemberBinding="{Binding DisplayName}" Width="200" />

                                            <GridViewColumn Header="Last Success"
                                                            DisplayMemberBinding="{Binding LastSuccessfulBuild.TimeStamp, Converter={StaticResource DateFromNowConverter}}" Width="90" />

                                            <GridViewColumn Header="Last Failure"
                                                            DisplayMemberBinding="{Binding LastFailedBuild.TimeStamp, Converter={StaticResource DateFromNowConverter}}" Width="90" />

                                            <GridViewColumn Header="Last Duration"
                                                            DisplayMemberBinding="{Binding LastCompletedBuild.Duration, StringFormat={}{0:mm\\:ss}}" Width="80" />

                                            <GridViewColumn Header="Culprits"
                                                            DisplayMemberBinding="{Binding LastBuild.Culprits, Converter={StaticResource UserDetailsConverter}}" Width="100" />

                                        </GridView.Columns>
                                    </GridView>
                                </ListView.View>
                            </ListView>

                        </DataTemplate>
                    </TabControl.ContentTemplate>
                </TabControl>

            </TabItem>

            <!-- USERS -->
            <TabItem Header="Users">
                <ListView ItemsSource="{Binding Users}"
                          SelectedItem="{Binding Main.SelectedUser, Source={StaticResource Locator}}">

                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="MouseDoubleClick">
                            <i:InvokeCommandAction
                                Command="{Binding Main.ShowUserInJenkinsCommand, Source={StaticResource Locator}}"
                                CommandParameter="{Binding Main.SelectedUser, Source={StaticResource Locator}}" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>

                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Details.FullName}" />
                            <GridViewColumn Header="Default project" DisplayMemberBinding="{Binding Job.Name}" />
                            <GridViewColumn Header="Last change"
                                            DisplayMemberBinding="{Binding LastActivity, Converter={StaticResource DateFromNowConverter}}" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </TabItem>
        </TabControl>
    </DockPanel>
</Window>